﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFSharp.Globalizer;

namespace Wpf_VirtualRouterPlus.Classes
{
    public class Setting
    {
        String _settingFile;
        String _language;

        public String Language
        {
            get { return _language; }
            set { _language = value; }
        }


        bool _startWithWindow;

        public bool StartWithWindow
        {
            get { return _startWithWindow; }
            set { _startWithWindow = value; }
        }

        public Setting()
        {
            _initialize();
        }

        public void setSettings(Setting setting_)
        {
            _language = setting_._language;
            _startWithWindow = setting_._startWithWindow;
        }

        public bool compare(Setting setting_)
        {
            bool result = true;
            result = this._language.Equals(setting_._language) && this._startWithWindow == setting_._startWithWindow;
            return result;
        }

        private void _initialize()
        {
            _settingFile = "Settings.ini";
            _language = "en-US";
            _startWithWindow = true;
        }

        public bool loadSettings()
        {
            try
            {
                string[] lines = File.ReadAllLines(_settingFile);
                String language = lines[0];
                var ci = new CultureInfo(language);
                _language = language;
                int startupType = int.Parse(lines[1]);
                _startWithWindow = (startupType == 1);
                return true;
            }
            catch (Exception ex)
            {
                _initialize();
                return false;
            }
        }

        public bool applySettings()
        {

            // apply language
            GlobalizedApplication.Instance.GlobalizationManager.SwitchLanguage(this._language);
            // apply startup registry
            if (_startWithWindow)
                WindowsUtils.addStartUpKey();
            else
                WindowsUtils.removeStartUpKey();

            return true;
        }

        public bool saveSettings()
        {
            String[] data = this.toArray();
            File.WriteAllLines(_settingFile, data);
            return true;
        }

        public String[] toArray()
        {
            String[] result = new String[2];
            result[0] = this._language;
            int startUp = 0;
            if (this._startWithWindow)
                startUp = 1;
            result[1] = startUp.ToString();
            return result;
        }
    }
}
